/*
 *  libKara - Library for basic instant messaging functions
 *  Copyright (C) 2007-2008 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */

package de.karnik.libkara.roster;

import java.util.Vector;

/**
 * The roster class implements the important roster functions. 
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class ImRoster {
	
	/**
	 * The roster instance.
	 */
	private static ImRoster imRoster = null;
	/**
	 * The contact storage.
	 */
	private Vector<ImContact> contacts = new Vector<ImContact>();
	/**
	 * The group storage.
	 */
	private Vector<String> groups = new Vector<String>();
	/**
	 * The roster listeners.
	 */
	private Vector<ImRosterListener> imRosterListeners = new Vector<ImRosterListener>( 1, 1 );
	
	/**
	 * Returns the roster instance.
	 * 
	 * @return The roster instance.
	 */
	public synchronized static ImRoster getInstance() {
		
		if( ImRoster.imRoster == null )
			ImRoster.imRoster = new ImRoster();
		
		return ImRoster.imRoster;
		
	}
	
	/**
	 * Standard constructor. Hidden. Caused by the singleton pattern.
	 */
	private ImRoster() {}
	
	/**
	 * Adds a im roster listener.
	 * 
	 * @param listener The im roster listener to add.
	 */
	public void addImRosterListener( ImRosterListener listener ) {
		this.imRosterListeners.add( listener );
	}
	
	/**
	 * Removes the given im roster listener.
	 * 
	 * @param listener The im roster listener to remove.
	 */
	public void removeImRosterLister( ImRosterListener listener ) {
		this.imRosterListeners.remove( listener );
	}
	
	/**
	 * Clears and rebuilds the roster.
	 */
	public void clearRoster() {
		this.contacts = new Vector<ImContact>();
		this.groups = new Vector<String>();
		
		this.rebuildRoster();
	}
	
	/**
	 * Rebuilds the roster. 
	 */
	public void rebuildRoster() {
		for( int i = 0; i < this.imRosterListeners.size(); i++ )
			this.imRosterListeners.get( i ).rosterChanged();
	}
	
	/**
	 * Adds a conact to the roster.
	 * 
	 * @param contact The contact to add.
	 */
	public void addImContact( ImContact contact ) {
		
		this.contacts.add( contact );
		
		String[] contactGroups = contact.getGroups();
		
		for( int i = 0; i < contactGroups.length; i++ ) {
			if( !this.groups.contains( contactGroups[ i ] ) )
				this.groups.add( contactGroups[i ] );
		}
	}
	
	/**
	 * Removes a contact from the roster.
	 * 
	 * @param contact The contact to remove.
	 */
	public void removeImContact( ImContact contact ) {
		this.contacts.remove( contact );
	}
	
	/**
	 * Returns a contact.
	 * 
	 * @param id The integer id of the contact to return.
	 * @return The wanted contact.
	 */
	public ImContact getImContact( int id ) {
		if( id < this.contacts.size() && id >= 0 )
			return this.contacts.get( id );
		
		return null;
	}
	
	/**
	 * Returns a contact.
	 * 
	 * @param id The string id of the contact to return.
	 * @return The wanted contact.
	 */
	public ImContact getImContact( String id ) {
		
		for( int i = 0; i < this.contacts.size(); i++ ) 
			if( this.contacts.get( i ).getUserID().equals( id ) )
				return this.contacts.get( i );
		
		return null;
		
	}
	
	/**
	 * Returns all contacts.
	 * 
	 * @return All available contacts.
	 */
	public ImContact[] getContacts() {
		return this.contacts.toArray( new ImContact[ this.contacts.size() ] );
	}
	
	/**
	 * Gets all contacts of the given groups.
	 * 
	 * @param group The group to get the contacts from.
	 * @return All contacts of the given groups. 
	 */
	public ImContact[] getContactsOfGroup( String group ) {
		Vector<ImContact> tempContacts = new Vector<ImContact>();
		
		for( int i = 0; i < this.contacts.size(); i++ ) {
			String[] groups = this.contacts.get( i ).getGroups();
			for( int j = 0; j < groups.length; j++ )
				if( groups[ j ].equals( group ) )
					tempContacts.add( this.contacts.get( i ) );
		}
		
		return tempContacts.toArray( new ImContact[ tempContacts.size() ] );
		
	}
	
	/**
	 * Returns all groups.
	 * 
	 * @return All available groups.
	 */
	public String[] getGroups() {
		return this.groups.toArray( new String[ this.groups.size() ] );
	}
	
}
